package fr.vvins.core.common.dto;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class DtoUtils {

	private final static Logger log = LoggerFactory.getLogger(DtoUtils.class);

    /**
     * Put pPropertyName of pBean at pValue
     * @param pBean
     * @param pPropertyName
     * @param pValue
     * @throws ItemNotFoundException
     */
    public static void setPropertyValue(Object pBean, String pPropertyName, Object pValue)  {
        try {
            BeanUtils.setProperty(pBean, pPropertyName, pValue);
        } catch (Throwable e) {
            if (log.isDebugEnabled()) {
                log.debug("AtgAssetUtils - Catch introspection exception : " + e.getMessage() + ". This property :" + pPropertyName
                        + " should be add in AtgAsset object or add to ignore list.");
            }
        }
    }
    
    /**
     * Get pPropertyName of pBean at pValue
     * @param pBean
     * @param pPropertyName
     * @param pValue
     * @throws ItemNotFoundException
     */
    public static Object getPropertyValue(Object pBean, String pPropertyName)  {
        try {
            return PropertyUtils.getProperty(pBean, pPropertyName);
        } catch (Throwable e) {
            if (log.isDebugEnabled()) {
                log.debug("AtgAssetUtils - Catch introspection exception : " + e.getMessage() + ". This property :" + pPropertyName
                        + " should be add in AtgAsset object or add to ignore list.");
            }
        }
        return null;
    }


    /**
     * Return a list of AtgItemPropert which describe target class.
     * @param target
     * @return
     */
    public static List<DtoProperty> getFields(BeanDtoInfo target) {
        List<DtoProperty> properties = new ArrayList<DtoProperty>();
        try {
            BeanInfo info = Introspector.getBeanInfo(target.getClass());
            PropertyDescriptor[] propertiesDesc = info.getPropertyDescriptors();
            for (PropertyDescriptor propertyDesc : propertiesDesc) {
            	if(propertyDesc.getWriteMethod() != null){
            		properties.add(new DtoProperty(propertyDesc));
            	}
            }
        } catch (IntrospectionException e) {
            if (log.isDebugEnabled()) {
                log.debug("DtotUtils - Catch introspection exception : " + e.getMessage() + ".");
            }
        }
        return properties;
    }
    
    public static boolean hasProperty(Object target, String propertyName){
    	
    	 try {
             BeanInfo info = Introspector.getBeanInfo(target.getClass());
             PropertyDescriptor[] propertiesDesc = info.getPropertyDescriptors();
             for (PropertyDescriptor propertyDesc : propertiesDesc) {
                if(propertyName.equals(propertyDesc.getName())){
                	return true;
                }
             }
         } catch (IntrospectionException e) {
             if (log.isDebugEnabled()) {
                 log.debug("DtotUtils - Catch introspection exception : " + e.getMessage() + ".");
             }
         }
    	return false;
    }


}
